added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2010 / VBAzureBingMaps / DAL / Travel.vb
blobc8fbabe01ce1b2722e11a5abd50ca35ee5fffe35
1 '***************************** Module Header ******************************\
2 '* Module Name: Travel.vb
3 '* Project: AzureBingMaps
4 '* Copyright (c) Microsoft Corporation.
5 '*
6 '* Partial class for the Travel EF entity.
7 '*
8 '* This source is subject to the Microsoft Public License.
9 '* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
10 '* All other rights reserved.
11 '*
12 '* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
13 '* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
14 '* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
15 '\**************************************************************************
18 Imports System.Data.Objects.DataClasses
19 Imports System.Data.Services
20 Imports System.Data.Services.Common
21 Imports System.IO
22 Imports Microsoft.SqlServer.Types
24 ''' <summary>
25 ''' The partial class for the Travel EF entity.
26 ''' Both PartitionKey and RowKey are part of data service key.
27 ''' Properties such as EntityState and EntityKey should not be passed to the client.
28 ''' The binary representation GeoLocation does not need to be passed to the client as well.
29 ''' </summary>
30 <DataServiceKey(New String() {"PartitionKey", "RowKey"})> _
31 <IgnoreProperties(New String() {"EntityState", "EntityKey", "GeoLocation"})> _
32 Partial Public Class Travel
33 Inherits EntityObject
34 Private _geoLocationText As String
36 ''' <summary>
37 ''' The text representation of the geo location, which is more user friendly.
38 ''' When Latitude and Longitude are modified, GeoLocationText will be modified as well.
39 ''' Client may upload an entity with Latitude/Longitude, but without GeoLocationText, so its value could be null.
40 ''' To avoid unintentionally setting GeoLocaionText to null, let's check the value in setter.
41 ''' </summary>
42 Public Property GeoLocationText() As String
43 Get
44 Return Me._geoLocationText
45 End Get
46 Set(ByVal value As String)
47 If Not String.IsNullOrEmpty(value) Then
48 Me._geoLocationText = value
49 End If
50 End Set
51 End Property
53 ' When either latitude or longitude changes, GeoLocationText must be changed as well.
54 ' The binary GeoLocation does not need to be changed, as it is only known by the database.
55 Private _latitude As Double
56 Public Property Latitude() As Double
57 Get
58 Return Me._latitude
59 End Get
60 Set(ByVal value As Double)
61 Me._latitude = value
62 Me.GeoLocationText = Me.LatLongToWKT(Me.Latitude, Me.Longitude)
63 End Set
64 End Property
66 Private _longitude As Double
67 Public Property Longitude() As Double
68 Get
69 Return Me._longitude
70 End Get
71 Set(ByVal value As Double)
72 Me._longitude = value
73 Me.GeoLocationText = Me.LatLongToWKT(Me.Latitude, Me.Longitude)
74 End Set
75 End Property
77 ''' <summary>
78 ''' Convert latitude and longitude to WKT.
79 ''' </summary>
80 Private Function LatLongToWKT(ByVal latitude As Double, ByVal longitude As Double) As String
81 Dim sqlGeography__1 As SqlGeography = SqlGeography.Point(latitude, longitude, 4326)
82 Return sqlGeography__1.ToString()
83 End Function
85 ''' <summary>
86 ''' GeoLocationText, Latitude, Longitude do not correspond to any column in the database.
87 ''' Geolocation (binary) corresponds to the GeoLocation column in the TravelView.
88 ''' If the binary GeoLocation changes, those values should be modified as well.
89 ''' This could happen when querying the entity.
90 ''' </summary>
91 Private Sub OnGeoLocationChanging(ByVal value As Global.System.Byte())
92 If value IsNot Nothing Then
93 Using ms As New MemoryStream(value)
94 Using reader As New BinaryReader(ms)
95 Dim sqlGeography As New SqlGeography()
96 sqlGeography.Read(reader)
97 Me.GeoLocationText = New String(sqlGeography.STAsText().Value)
98 Me.Latitude = sqlGeography.Lat.Value
99 Me.Longitude = sqlGeography.[Long].Value
100 End Using
101 End Using
102 End If
103 End Sub
104 End Class